<?php

require_once('load.php');
$smarty->assign("s1","seans");
$smarty->assign("s2","");
$smarty->assign("nawig1",array("nazwa" => "Zarządzanie seansów","url" => "seans.php"));

if(LOGGED == false)header('Location: ./index.php');


if(isset($_GET['action'])){
	switch ($_GET['action']) {
		case 'dodaj_seans':
			// $_get['id'] - identyfikator klienta 
			if(count($_POST)){
				if(isset($_POST['termin_godzina']) && preg_match("/^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/",$_POST['termin_godzina'])){
					$termin = $_POST['termin'];
					$termin.= " ".$_POST['termin_godzina'];
				}else{
					header('Location: ./seans.php?action=failed&zly_format_termin_h');
				}
				if($sql->query("insert into `seans` (`id_film`, `id_sala`, `termin`, `cena_biletu`) values ('".$_POST['filmy']."','".$_POST['sale']."','".$termin."','".$_POST['cena_biletu']."') ")){
					header('Location: ./seans.php?action=success&k=dodano_seans');
				}else{
					header('Location: ./seans.php?action=failed');
				}
			}
			$smarty->assign("s2","dodaj_seans");
			break;	
		case 'usun_seans':
			if(isset($_GET['id'])){
				if($sql->query('delete from `seans` WHERE `id_seans`=\''.$_GET['id'].'\'')){
					header('Location: ./seans.php?action=success&k=usunieto_seans');
				}else{
					header('Location: ./seans.php?action=failed');
				}
			}
			break;		
		case 'edytuj_seans':
			if(isset($_GET['id'])){
				
				if(isset($_POST) && count($_POST)){
					if(isset($_POST['termin_godzina']) && preg_match("/^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$/",$_POST['termin_godzina'])){
						$termin = $_POST['termin'];
						$termin.= " ".$_POST['termin_godzina'];
					}else{
						header('Location: ./seans.php?action=failed&zly_format_termin_h');
					}
			
					if($sql->query('UPDATE `seans` SET `id_film`=\''.$_POST['filmy'].'\', `id_sala`=\''.$_POST['sale'].'\', `termin`=\''.$termin.'\', `cena_biletu`=\''.$_POST['cena_biletu'].'\' WHERE `id_seans`=\''.$_GET['id'].'\' ')){
						header('Location: ./seans.php?action=success&k=zaktualizowano_film');
					}else{
						header('Location: ./seans.php?action=failed');
					}
					
				}
				$ar = $sql->QueryGetFetch("SELECT * FROM `seans` WHERE `id_seans`='".$_GET['id']."'");
				$ar = $ar[0];
				$smarty->assign("seans",$ar);
				$smarty->assign("gatunki",$sql->QueryGetFetch("SELECT * FROM `gatunek`"));
				$smarty->assign("s2","edytuj_seans");
			}		
			break;
		case 'failed':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'usunieto_sala'){
					$smarty->assign("komunikat","Usunięto");
				}else{
					$smarty->assign("komunikat","Wstąpił błąd");
				}
			}else{
				$smarty->assign("komunikat","Wstąpil błąd");
			}
			
			break;
		case 'success':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'dodano_sala'){
					$smarty->assign("komunikat","Pomyslnie dodano sala");
				}else if($_GET['k'] == 'usunieto_sala'){
					$smarty->assign("komunikat","Usunięto");
				}else{
					$smarty->assign("komunikat","Pomyslnie zakonczono");
				}
			}else{
				$smarty->assign("komunikat","Pomyslnie zakonczono");
			}
			break;
		default:

			break;
	}
}

$smarty->assign("seansy",$sql->QueryGetFetch("select * from `seans`"));
$smarty->assign("filmy",$sql->QueryGetFetch("select * from `film`"));
$smarty->assign("sale",$sql->QueryGetFetch("select * from `sala`"));

$smarty->display('templates/index.tpl');
?>